﻿package fpc.controls;
import flash.display.DisplayObjectContainer;
import flash.display.Graphics;
import flash.display.Shape;
import flash.events.Event;
import fpc.ALayoutable;
import fpc.utils.Maths;

class Spacer extends ALayoutable
{
	var _shape:Shape;//?
	
	public function new(width:Float = 0, height:Float = 0) 
	{
		super();
		_width = width;
		_height = height;
		
		_shape = new Shape();
	}
	
	public function setXSize(width:Float, ?percentsWidth:Float, ?minWidth:Float, ?maxWidth:Float)
	{
		if (!Math.isNaN(width))
		{
			_width = width;
		}
		_minWidth = (minWidth != null && !Math.isNaN(minWidth)) ? minWidth : 0;
		_maxWidth = maxWidth != null ? maxWidth : Math.NaN;
		this.percentsWidth = percentsWidth != null ? percentsWidth : Math.NaN;
		return this;
	}
	
	public function setYSize(height:Float, ?percentsHeight:Float, ?minHeight:Float, ?maxHeight:Float)
	{
		if (!Math.isNaN(height))
		{
			_height = height;
		}
		_minHeight = (minHeight != null && !Math.isNaN(minHeight)) ? minHeight : 0;
		_maxHeight = maxHeight != null ? maxHeight : Math.NaN;
		this.percentsHeight = percentsHeight != null ? percentsHeight : Math.NaN;
		return this;
	}
	
	public override function addTo(parent:DisplayObjectContainer, index = Maths.INT_NULL)
	{
		parent.addChild(_shape);
		parent.addEventListener(Event.ENTER_FRAME, onEnterFrame);
	}
	
	public override function removeFrom(parent:DisplayObjectContainer)
	{
		parent.removeChild(_shape);
	}
	
	function onEnterFrame(e:Event)
	{
		var g = _shape.graphics;
		g.clear();
		g.lineStyle(0, 0xe00000);
		g.beginFill(width < maxWidth || height < maxHeight ? 0x0000ff : 0xff0000);
		g.drawRect(x, y, width, height);
		g.endFill();
		g.lineStyle(0, 0xe00000, .5);
		g.drawRoundRect(x - 2, y - 2, width + 4, height + 4, 4);
	}
	
	public var maxWidth(getMaxWidth, setMaxWidth):Float;
	var _maxWidth:Float;
	function getMaxWidth()
	{
		return _maxWidth;
	}
	function setMaxWidth(value)
	{
		_maxWidth = value;
		heraldResize.dispatch(this);
		return value;
	}
	
	public var maxHeight(getMaxHeight, setMaxHeight):Float;
	var _maxHeight:Float;
	function getMaxHeight()
	{
		return _maxHeight;
	}
	function setMaxHeight(value)
	{
		_maxHeight = value;
		heraldResize.dispatch(this);
		return value;
	}
	
	public var minWidth(getMinWidth, setMinWidth):Float;
	var _minWidth:Float;
	function getMinWidth()
	{
		return _minWidth;
	}
	function setMinWidth(value)
	{
		_minWidth = value;
		heraldResize.dispatch(this);
		return value;
	}
	
	public var minHeight(getMinHeight, setMinHeight):Float;
	var _minHeight:Float;
	function getMinHeight()
	{
		return _minHeight;
	}
	function setMinHeight(value)
	{
		_minHeight = value;
		heraldResize.dispatch(this);
		return value;
	}
	
	override function getWidth()
	{
		if (_width < _minWidth)
		{
			return _minWidth;
		}
		else if (_width > _maxWidth)
		{
			return _maxWidth;
		}
		else
		{
			return _width;
		}
	}
	override function setWidth(value)
	{
		if (!Math.isNaN(value))
		{
			_width = value;
			heraldResize.dispatch(this);
		}
		return value;
	}
	
	override function getHeight()
	{
		if (_height < _minHeight)
		{
			return _minHeight;
		}
		else if (_height > _maxHeight)
		{
			return _maxHeight;
		}
		else
		{
			return _height;
		}
	}
	override function setHeight(value)
	{
		if (!Math.isNaN(value))
		{
			_height = value;
			heraldResize.dispatch(this);
		}
		return value;
	}
	
	override function setPercentsWidth(value)
	{
		super.setPercentsWidth(value);
		heraldResize.dispatch(this);
		return _percentsWidth;
	}
	
	override function setPercentsHeight(value)
	{
		super.setPercentsHeight(value);
		heraldResize.dispatch(this);
		return _percentsHeight;
	}
}